//
//  ExploreViewModel.swift
//  Twitter
//
//  Created by Hoa.wang on 2022/4/7.
//

import SwiftUI

class ExploreViewModel: ObservableObject {
    @Published var users = [User]()
    @Published var seachText = ""
    
    var searchableUsers: [User] {
        if seachText.isEmpty {
            return users
        }
        else {
            let lowercasedQuery = seachText.lowercased()
            return users.filter {
                $0.username?.contains(lowercasedQuery) ?? false ||
                $0.fullname?.lowercased().contains(lowercasedQuery) ?? false
            }
        }
    }
    
    let service = UserService()
    
    init() {
        fetchUsers()
    }
    
    func fetchUsers() {
        service.fetchUsers { users in
            DispatchQueue.main.async {
                self.users = users
            }
        }
    }
}
